#!/usr/bin/env python
from multiplexer.multiplexer_constants import peers, types
from multiplexer.clients import BaseMultiplexerServer
import settings, cPickle, collections


class DiodeCatcher(BaseMultiplexerServer):
    def __init__(self, addresses):
        super(DiodeCatcher, self).__init__(addresses=addresses, type=peers.DIODE_CATCHER)
        #self.buffer_size = int(self.conn.query(message="DiodeCatcherBufferSize", type=types.DICT_GET_REQUEST_MESSAGE).message)
        self.buffer = collections.deque()
        self.buffer = collections.deque()

<<<<<<< .mine
    def add(self, value):
        
        
        blink = variables_pb2.Blink()
        blink.ParseFromString(value)
        buffer.append(blink)
        sampleVector = variables_pb2.SampleVector()
        #print "UWAGA"
        sampleVector.ParseFromString(value)
        #print sampleVector
        values = sampleVector.samples

        # buffer[i] = buffer_size samples from channel i
        i = 0
        for s in values:
            self.buffer[i].append(s)
            if len(self.buffer[i]) > self.buffer_size:
                self.buffer[i].popleft()
            i += 1

        

=======
>>>>>>> .r21
    def handle_message(self, mxmsg):
        if mxmsg.type == types.DIODE_REQUEST:
            self.send_message(message = self.buffer.popleft(), type = types.DIODE_RESPONSE)
        elif mxmsg.type == types.DIODE_MESSAGE:
            self.buffer.append(mxmsg.message)
            self.no_response()


if __name__ == "__main__":
    DiodeCatcher(settings.MULTIPLEXER_ADDRESSES).loop()
